Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@putout/compare

Package Overview
Dependencies
Maintainers
1
Versions
133
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@putout/compare

compare ast-nodes

  • 7.13.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
11K
increased by27.81%
Maintainers
1
Weekly downloads
 
Created
Source

@putout/compare NPM version Dependency Status

Compare AST-nodes.

Install

npm i @putout/compare

API

getTemplateValues(node, template)

  • node - AST-node or code that will be generated;
  • template - template string with template variables;
const {operator} = require('putout');
const {template} = operator;
const node = template.ast('const [] = a');

getTemplateValues(node, 'const __array = array');
// returns
({
    __array: {
        type: 'ArrayPattern',
        //...etc
    },
});

compare (node, baseNode)

  • node - AST-node or code that will be generated;
  • baseNode AST-node with support of template variables.
Supported template variables:
__

Any node.

compare('const x = data', 'const __ = __');
compare('const {x} = data', 'const __ = __');
compare('const x = {data}', 'const __ = __');
// returns
true;
__object

ObjectPattern or ObjectExpression with any count of properties.

compare('const {} = data', 'const __object = __');
compare('const {hello} = data', 'const __object = __');
// returns
true;
__array

ArrayPattern or ArrayExpression with any count of elements.

compare('const [] = data', 'const __array = __');
compare('const [hello] = data', 'const __array = __');
compare('const hello = [data]', 'const __ = __array');
// returns
true;
__args, __args_a

Any count of arguments:

compare('(a, b, c) => {}', '(__args) => {}');
compare('(a, b) => {}', '(__args) => {}');
compare('() => {}', '(__args) => {}');
// returns
true;
__imports

Any count of imports:

compare('import React, {Component} from "react"', 'import __imports from "react"');
"__"

Any string literal.

compare('const a = "hello"', 'const __ = "__"');
__a

Linked literal

compare('const __a = "hello"', 'const __a = "hello"');
"__a"

Linked string literal.

compare('const a = "hello"', 'const __a = "__b"');
__a

Linked template literal.

compare('const a = `hello`', 'const __a = `__b`');
// returns
true;
__body

Any BlockStatement.

compare('const a = () => 0', 'const a = () => __body');
// returns
false;

compare('const a = () => {}', 'const a = () => __body');
// returns
true;
__nop

Any Function with no arguments and empty body;

compare('const a = () => {}', 'const __a = __nop');
// returns
true;

compare('const a = async () => {}', 'const a = __nop');
// returns
true;
__identifier

Any `Identifier

compare('const a = 5', 'const __identifier = 5');
// returns
true;
/__a/

Any regexp

compare('const a = /hello/g', 'const a = /__a/');
// returns
true;

License

MIT

Keywords

FAQs

Package last updated on 04 Sep 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc